<template>
  <!-- 标签 -->
  <div class="container">
    <!-- 卡片区域 -->
    <el-card class="card-box">
      <!--面包屑导航 -->
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        class="breadcrumb"
        v-show="queryUrl.name"
      >
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{ queryUrl.name }}</el-breadcrumb-item>
        <el-breadcrumb-item>标签管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 表单区域 -->
      <el-form ref="form" :model="queryForm" label-width="70px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="标签名称">
              <el-input
                class="text-input"
                v-model="queryForm.tagName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="queryForm.state" placeholder="请选择">
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="(item, index) in status"
                  :key="index"
                ></el-option>
              </el-select>
              <el-button size="mini" class="btn" @click="clearOnClike"
                >清除</el-button
              >
              <el-button type="primary" size="mini" @click="queryOnClick"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-form-item>
              <el-button
                type="text"
                size="small"
                icon="el-icon-back"
                v-show="queryUrl.name"
                @click="returnOnClick"
                >返回学科</el-button
              >
              <el-button
                type="success"
                size="small"
                icon="el-icon-edit"
                @click="addTags"
                class="btnOncLick"
                >新增标签</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 警告行 -->
      <el-alert
        :title="`消息一共${total}条`"
        class="alert"
        type="info"
        :closable="false"
        show-icon
      >
      </el-alert>
      <!-- 列表 -->
      <el-table :data="tagsList" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">{{ '已开启' }}</span>
            <span v-else>{{ '已禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="updateState(scope.row)">{{
              scope.row.state === 1 ? '禁用' : '启用'
            }}</el-button>
            <el-button
              type="text"
              :disabled="scope.row.state === 1"
              @click="editOnClick(scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state === 1"
              @click="editDelOnClicke(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="page-size"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.page"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="queryForm.pagesize"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 新增标签弹框 -->
      <el-dialog title="新增目录" :visible.sync="addDialogVisible" width="30%">
        <el-form
          :model="addRuleForm"
          :rules="addRules"
          ref="addRuleFormRef"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="所属学科" v-show="!queryUrl.subjectID">
            <el-select
              v-model="addRuleForm.subjectID"
              placeholder="请选择活动区域"
              class="selet"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="(item, index) in easyList"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="addRuleForm.tagName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="enterOnClick">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改弹框 -->
      <el-dialog title="修改目录" :visible.sync="editDialogVisible" width="30%">
        <el-form
          :model="editRuleForm"
          :rules="addRules"
          ref="editRuleFormRef"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item
            label="所属学科"
            style="width: 100%"
            v-show="!queryUrl.subjectID"
          >
            <el-select
              v-model="editRuleForm.subjectID"
              placeholder="请选择活动区域"
              style="width: 100%"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="(item, index) in easyList"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName" style="width: 100%">
            <el-input
              v-model="editRuleForm.tagName"
              style="width: 100%"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editEnterOnClick">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, changeState, add, update, remove } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects.js'
import { status } from '@/api/hmmm/constants.js'
export default {
  name: 'Tags',
  data() {
    return {
      queryForm: {
        page: 1,
        pagesize: 10
      },
      // 标签列表
      tagsList: [],
      total: 0,
      status: status,
      // 控制添加标签弹框是否显示
      addDialogVisible: false,
      editDialogVisible: false,
      // 路径参数
      queryUrl: {},
      // 添加标签
      addRuleForm: {
        subjectID: '',
        tagName: ''
      },
      // 修改
      editRuleForm: {
        id: '',
        subjectID: '',
        tagName: ''
      },
      // 简单列表
      easyList: [],
      addRules: {
        tagName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    // 获取简单学科列表
    this.getEasyList()
    // 获取路径参数
    this.queryUrl.subjectID = this.$route.query.id
    this.queryUrl.name = this.$route.query.name
    console.log(this.queryUrl)
    if (this.queryUrl.subjectID) {
      this.queryForm.subjectID = this.queryUrl.subjectID
      this.getList()
    }
  },
  methods: {
    // 获取数据列表
    async getList() {
      try {
        const { data } = await list(this.queryForm)
        this.tagsList = data.items
        this.total = data.counts
      } catch (err) {
        this.$message('获取数据失败')
      }
    },
    // 每页显示条数发生改变
    handleSizeChange(val) {
      this.queryForm.pagesize = val
      this.getList()
    },
    // 当前页面发生改变
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.getList()
    },
    // 清除
    clearOnClike() {
      this.queryForm = this.$options.data().queryForm
      this.getList()
    },
    // 查询
    queryOnClick() {
      this.getList()
    },
    // 启用还是禁用
    async updateState(item) {
      const stat = item.state === 1 ? 0 : 1

      try {
        await changeState({
          id: item.id,
          state: stat
        })
        this.getList()
      } catch (err) {
        this.$message('修改失败')
      }
    },
    // 添加标签
    addTags() {
      this.addDialogVisible = true
    },
    // 确定添加
    enterOnClick() {
      if (this.queryUrl.subjectID) {
        //  const state = this.addRuleForm.isFrontDisplay ? 1:0
        this.addRuleForm.subjectID = parseInt(this.queryUrl.subjectID)
        this.$refs.addRuleFormRef.validate(async valid => {
          if (!valid) return
          try {
            await add(this.addRuleForm)
            this.getList()
            this.addDialogVisible = false
            this.$message.success('添加成功')
          } catch (err) {
            this.$message('添加失败')
          }
        })
      } else {
        const state = this.addRuleForm.isFrontDisplay ? 1 : 0
        this.$refs.addRuleFormRef.validate(async valid => {
          if (!valid) return
          try {
            await add(this.addRuleForm)
            this.getList()
            this.addDialogVisible = false
            this.$message.success('添加成功')
          } catch (err) {
            this.$message('添加失败')
          }
        })
      }
    },
    // 获取简单列表
    async getEasyList() {
      try {
        const { data } = await simple()
        this.easyList = data
      } catch (err) {
        this.$message('获取失败')
      }
    },
    // 修改
    editOnClick(item) {
      this.editDialogVisible = true
      this.editRuleForm.subjectID = item.subjectID
      this.editRuleForm.tagName = item.tagName
      this.editRuleForm.id = item.id
    },
    // 确定修改
    async editEnterOnClick() {
      this.$refs.editRuleFormRef.validate(async valid => {
        if (!valid) return
        try {
          await update(this.editRuleForm)
          this.editDialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        } catch (err) {
          this.$message('修改失败')
        }
      })
    },
    // 删除标签
    editDelOnClicke(item) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // console.log(11)
          try {
            await remove(item)
            this.getList()
          } catch (err) {
            this.$message('操作失败')
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          // console.log(22)
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击返回
    returnOnClick() {
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 15px;
  .breadcrumb {
    margin-bottom: 20px;
    color: #000 !important;
    padding: 0 0 20px 0;
    border-bottom: 1px solid #ebeef5;
    /deep/ .is-link {
      color: #000 !important;
    }
  }
  .from {
    display: flex;
  }
  .text-input {
    width: 200px;
  }
  .selet {
    width: 100%;
  }
  .page-size {
    float: right;
    padding: 15px;
  }
  .btn {
    margin-left: 20px;
  }
  .alert {
    margin-bottom: 10px;
  }
  /deep/.el-dialog__footer {
    text-align: right;
  }
}
</style>
